<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk" :loading="buttonok">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
	<a-row>

	<a-col :span="12">
    <a-form-item label="仓库编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :disabled="disableSubmit" v-decorator="['warehouseId', validatorRules.warehouseId]" placeholder="请输入仓库编号"></a-input>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="库存编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :disabled="disableSubmit" v-decorator="['inventoryId']" placeholder="请输入库存编号"></a-input>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="已使用冷冻" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="disableSubmit" v-decorator="['physicalFreeze']" placeholder="请输入已使用冷冻" style="width: 100%"/>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="已使用冷藏" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="disableSubmit" v-decorator="['physicalRefrigerate']" placeholder="请输入已使用冷藏" style="width: 100%"/>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="已使用恒温" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="disableSubmit" v-decorator="['physicalConstant']" placeholder="请输入已使用恒温" style="width: 100%"/>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="已使用常温" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :disabled="disableSubmit" v-decorator="['physicalNormal']" placeholder="请输入已使用常温" style="width: 100%"/>
        </a-form-item>
	</a-col>
	<a-col :span="12">
    <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注"/>
        </a-form-item>
	</a-col>

 </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "CrmInventoryHistoryModal",
    components: { 
    },
    data () {
      return {
        disableSubmit: false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          warehouseId: {
            rules: [
              { required: true, message: '请输入仓库编号!'},
            ]
          },
        },
        url: {
          add: "/online/api/createCrmInventoryHistory",
          edit: "/online/api/updateCrmInventoryHistory",
          get: "/online/api/getCrmInventoryHistory",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
		this.visible = true;
		this.model = {};
        if(record.id){
	      httpAction(this.url.get,{id:record.id},"post").then((res)=>{
	         if(res.success){
		        this.model = Object.assign({}, res.result);
		        this.$nextTick(() => {
		           this.form.setFieldsValue(pick(this.model,'warehouseId','inventoryId','physicalFreeze','physicalRefrigerate','physicalConstant','physicalNormal','remark','createUserId','createTime'))
		        })   
	         }
	      })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'warehouseId','inventoryId','physicalFreeze','physicalRefrigerate','physicalConstant','physicalNormal','remark','createUserId','createTime'))
      },

      
    }
  }
</script>
